#include "register.h"
#include "ui_register.h"
#include<QMessageBox>
#include "loginwidget.h"


Register::Register(QDialog *parent)
    : QDialog(parent)
    , ui(new Ui::Register)
{
    ui->setupUi(this);

    //设置用户名
    ui->lineEdit->setPlaceholderText("请输入用户名");
    //设置密码
    ui->lineEdit_2->setPlaceholderText("请输入密码");
    ui->lineEdit_2->setEchoMode(QLineEdit::Password);
    //设置姓名
    ui->lineEdit_3->setPlaceholderText("请输入姓名");

    //设置年龄
    ui->spinBox->setRange(1,100);
    ui->spinBox->setSingleStep(1);
    //设置性别
    ui->comboBox->addItem("男");
    ui->comboBox->addItem("女");
}

Register::~Register()
{
    delete ui;
}


void Register::on_pushButton_2_clicked()
{
    this->close();
    emit finish(); // 发射信号
}

void Register::on_pushButton_clicked()
{
    if(ui->lineEdit->text()==NULL||ui->lineEdit_2->text()==NULL|ui->lineEdit_3->text()==NULL){
        QMessageBox::information(this,"提示信息","请填写完整信息");
    }else{
        // 获取用户输入的数据
            QString username = ui->lineEdit->text();
            QString password = ui->lineEdit_2->text();
            QString name = ui->lineEdit_3->text();
            QString role = QString::number(DOCTOR);
            int age = ui->spinBox->text().toInt();
            QString gender = ui->comboBox->currentText();
            QString contactNumber = ui->lineEdit_4->text();
            QString email = ui->lineEdit_5->text();
        // 构建要发送的数据
            QString data = username + "," + password + ","  + name + "," + role + "," + QString::number(age) + ","+ gender + "," + contactNumber + "," + email;
        // 连接到服务器
            socket->connectToHost("localhost", 8081);
            if (socket->waitForConnected(3000)) {
         // 发送数据
           socket->write(data.toUtf8());
           socket->write("\n");
           socket->flush();

           // 等待服务器响应
           if (socket->waitForReadyRead(3000)) {
               QString response = socket->readAll();
               QMessageBox::information(this, "注册", "Registration successful!\nServer response: " + response);
           } else {
               QMessageBox::warning(this, "注册", "Failed to receive server response.");
           }
           socket->disconnectFromHost();
       } else {
           QMessageBox::warning(this, "注册", "Failed to connect to server.");
       }
    }

}




